﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ILeaveRecord
    {
        public DataSet LeaveRecord_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveRecord_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool LeaveRecord_CheckExist(int EmployeeID, int month, int year)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_CheckExist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public LeaveRecordInfo LeaveRecord_GetInfo(int LRID)
        {
            LeaveRecordInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ID", LRID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new LeaveRecordInfo();
                    retVal.ID = Convert.ToInt32(dr["LeaveRecord_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["LeaveRecord_EmployeeID"]);
                    retVal.FromDate = Convert.ToDateTime(dr["LeaveRecord_FromDate"]);
                    retVal.ToDate = Convert.ToDateTime(dr["LeaveRecord_ToDate"]);
                    retVal.ALDays = Convert.ToDecimal(dr["LeaveRecord_ALDays"]);
                    retVal.CODays = Convert.ToDecimal(dr["LeaveRecord_CODays"]);
                    retVal.ALBefore = Convert.ToDecimal(dr["LeaveRecord_ALBefore"]);
                    retVal.COBefore = Convert.ToDecimal(dr["LeaveRecord_COBefore"]);

                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveRecord_GetOne(int LRID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_GetOne", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ID", LRID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public int LeaveRecord_Insert(LeaveRecordInfo _lRInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LeaveRecord_EmployeeID", _lRInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_FromDate", _lRInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ToDate", _lRInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ALDays", _lRInfo.ALDays);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_CODays", _lRInfo.CODays);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ALBefore", _lRInfo.ALBefore);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_COBefore", _lRInfo.COBefore);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool LeaveRecord_Update(LeaveRecordInfo _lRInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ID", _lRInfo.ID);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_EmployeeID", _lRInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_FromDate", _lRInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ToDate", _lRInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ALDays", _lRInfo.ALDays);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_CODays", _lRInfo.CODays);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ALBefore", _lRInfo.ALBefore);
            dbCmd.Parameters.AddWithValue("@LeaveRecord_COBefore", _lRInfo.COBefore);
            try
            {
                dbConn.Open();
                dbCmd.ExecuteNonQuery();
                retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool LeaveRecord_Delete(int LRID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LeaveRecord_ID", LRID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveRecord_GetWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveRecord_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LeaveRecord_EmployeeID", EmployeeID);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

